package com.story.modular.app.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.story.common.constant.factory.PageFactory;
import com.story.common.exception.BizExceptionEnum;
import com.story.common.exception.BussinessException;
import com.story.common.persistence.model.PayDetail;
import com.story.common.persistence.model.SysOffline;
import com.story.common.persistence.model.User;
import com.story.common.persistence.model.VipType;
import com.story.core.base.controller.BaseController;
import com.story.core.shiro.ShiroKit;
import com.story.core.shiro.ShiroUser;
import com.story.modular.app.dao.SysOfflineMapper;
import com.story.modular.app.dao.VipTypeMapper;
import com.story.modular.app.service.PayDetailService;
import com.story.modular.system.dao.UserMgrDao;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Pyx
 * @PACKAGE_NAME : com.story.modular.app.controller
 * @FileName: OfflineController
 * @date 2020/5/6 11:41
 */
@Controller
@RequestMapping("offline")
@Api(value = "第二批 下线管理", tags = "第二批 下线管理")
public class OfflineController extends BaseController {
    @Autowired
    private SysOfflineMapper sysOfflineMapper;
    @Autowired
    private UserMgrDao userMgrDao;
    @Autowired
    private VipTypeMapper vipTypeMapper;
    @Autowired
    private PayDetailService payDetailService;
    private static String PREFIX = "/system/offline/";

    @RequestMapping("/index")
    public String index() {
        return PREFIX + "offline.html";
    }
    @ApiOperation("列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Object list() throws ParseException {
        Page<SysOffline> page = new PageFactory<SysOffline>().defaultPage();
        return sysOfflineMapper.getList(page);
    }

    @ApiOperation("下线添加上线 参数 name 上线的名字或手机号 channel 渠道 equipment 设备 location 位置")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Object add(String name,String channel,String equipment,String location) {
        ShiroUser shiroUser = ShiroKit.getUser();
        if(shiroUser !=null) {
            Integer id = shiroUser.getId();
            Integer select = sysOfflineMapper.selectByUserId(id);
            if(select > 0){
                throw new BussinessException(BizExceptionEnum.DONT_MSZJ);
            }
            User user = userMgrDao.getByNameOrPhone(name);
            if(user.getId().equals(id)){
                throw new BussinessException(BizExceptionEnum.DONT_TAOWA);
            }
            SysOffline sysOffline = new SysOffline();
            sysOffline.setUserId(id);
            sysOffline.setUsername(shiroUser.getName());
            sysOffline.setUpUserId(user.getId());
            sysOffline.setChannel(channel);
            sysOffline.setEquipment(equipment);
            sysOffline.setLocation(location);
            sysOfflineMapper.insertSelective(sysOffline);
            Integer typeId = vipTypeMapper.get7Days().getId();
            PayDetail payDetail = new PayDetail();
            payDetail.setPayType("贈送");
            payDetail.setVipType(typeId);
            int r1 = payDetailService.save(payDetail,null);
            int r2 = payDetailService.save(payDetail,user.getId());
            if (r1 > 0) {
                Map<String,Object> map = new HashMap<String,Object>();
                map.put("message","success");
                map.put("status","200");
                return ResponseEntity.ok(map);
            } else {
                throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
            }
        }else{
            throw new BussinessException(BizExceptionEnum.NO_PERMITION);
        }

    }
}
